package com.yzf.wisecoder.wisecoderservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzf.uidgeneratorcore.impl.CachedUidGenerator;
import com.yzf.wisecoder.wisecodercommon.exception.RRException;
import com.yzf.wisecoder.wisecodercommon.response.Query;
import com.yzf.wisecoder.wisecodercommon.utils.SecurityUtils;
import com.yzf.wisecoder.wisecoderservice.service.*;
import com.yzf.wisecoder.wisecoderstorage.mysql.dao.ProjectDao;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ProjectEntity;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ProjectUserEntity;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author jianghuiyong
 * @email jianghuiyong@yunzhangfang.com
 * @date 2020-05-06 17:43:12
 */
@Service("projectService")
@AllArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {


    private ProjectUserService projectUserService;
    private RoleService roleService;
    private CachedUidGenerator cachedUidGenerator;
    private ViewDbService viewDbService;
    private ViewConfigService viewConfigService;
    private ViewDatasourceService viewDatasourceService;


    @Override
    public IPage<ProjectEntity> queryPage(Map<String, Object> params) {
        Page<ProjectEntity> page = new Query<ProjectEntity>(params).getPage();
        IPage<ProjectEntity> iPage = baseMapper.queryProjectPage(page, params, SecurityUtils.getUserId(), SecurityUtils.getTenantId());
        return iPage;
    }

    @Override
    public List<ProjectEntity> queryList(Map<String, Object> params) {
        if (roleService.hasAdmin(SecurityUtils.getUserId(), SecurityUtils.getTenantId())) {
            return list(new QueryWrapper<ProjectEntity>().eq("deleted", Boolean.FALSE)
                    .eq("tenant_id", SecurityUtils.getTenantId()));
        } else {
            return baseMapper.queryProjectList(params, SecurityUtils.getUserId(), SecurityUtils.getTenantId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateProject(ProjectEntity projectEntity) {
        ProjectEntity projectEntity1 = getOne(new QueryWrapper<ProjectEntity>()
                .eq("project_name", projectEntity.getProjectName())
                .eq("tenant_id", SecurityUtils.getTenantId()));
        if (projectEntity1 != null && (projectEntity.getProjectId() == null
                || projectEntity.getProjectId().longValue() != projectEntity1.getProjectId().longValue())) {
            throw new RRException("项目名称不能重复");
        }
        if (projectEntity.getProjectId() == null) {
            Long userId = SecurityUtils.getUserId();
            projectEntity.setProjectId(cachedUidGenerator.getUID())
                    .setCreator(userId)
                    .setCreateTime(LocalDateTime.now())
                    .setDeleted(Boolean.FALSE)
                    .setTenantId(SecurityUtils.getTenantId());
            save(projectEntity);
            ProjectUserEntity projectUserEntity = new ProjectUserEntity();
            projectUserEntity.setProjectId(projectEntity.getProjectId())
                    .setTenantId(SecurityUtils.getTenantId())
                    .setUserId(userId)
                    .setCreator(userId)
                    .setCreateTime(LocalDateTime.now());
            projectUserService.save(projectUserEntity);
        } else {
            updateById(projectEntity);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Long> projectIds) {
        removeByIds(projectIds);
        projectUserService.removeByProjectIds(projectIds);
        viewDbService.removeByProjectId(projectIds);
        viewConfigService.removeByProjectId(projectIds);
        viewDatasourceService.removeByProjectId(projectIds);
        return true;
    }
}